using Domain.Entities;
using Domain.Entities.Enums;
using Domain.Mail;
using Domain.Misc;
using Domain.Services;
using System;
using UserInterface.Misc;

namespace UserInterface
{
    public partial class Register : BoardPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string pageTitle = (string)GetLocalResourceObject("Title");
            MasterPage.TitleGenerator.PageTitle = pageTitle;
            MasterPage.HeaderText = pageTitle;
            MasterPage.AddPathItem(pageTitle, "Register.aspx");
        }

        protected void RegisterButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                string username = UsernameTextBox.Text.Trim();
                string email = EmailTextBox.Text.Trim();
                string password = PasswordTextBox.Text.Trim();
                string resourcekey = null;

                User user = UserService.GetByUsername(username);

                if (user != null)
                {
                    MasterPage.Messages.AddCritical((string)GetLocalResourceObject("UsernameAlreadyExists"));
                }
                else
                {
                    UserStatus status;

                    if (BoardSettingsManager.Instance.Settings.AccountActivationByEmail)
                    {
                        status = UserStatus.AwaitingActivationByEmail;
                        resourcekey = "RegisterAwaitingActivationByEmail";
                    }
                    else if (BoardSettingsManager.Instance.Settings.AccountActivationByAdmin)
                    {
                        status = UserStatus.AwaitingActivationByAdmin;
                        resourcekey = "RegisterAwaitingActivationByAdmin";
                    }
                    else
                    {
                        status = UserStatus.Activated;
                        resourcekey = "RegisterSuccessful";
                    }

                    User newUser = UserService.NewUser(username, email, password, status, null);

                    if (status == UserStatus.AwaitingActivationByEmail)
                    {
                        MailParser mailParser = new MailParser(Server.MapPath("~/Emails/AccountActivation.xml"));
                        mailParser.AddBodyTag("Username", newUser.Username);
                        mailParser.AddBodyTag("ActivationUrl", "http://" + Request.ServerVariables["HTTP_HOST"] + "/ActivateAccount.aspx?userId=" + newUser.Id + "&activationKey=" + newUser.ActivationKey);

                        MailSender mailSender = new MailSender(mailParser);
                        mailSender.AddRecipient(newUser.Email);
                        mailSender.Send();
                    }

                    DieMessage(DieMessageType.Information, resourcekey);
                }
            }
            else
            {
                MasterPage.Messages.AddCritical(Resources.Resource.InvalidFormData);
            }
        }
    }
}
